package com.autonavi.yunda.yunji.common.web;

import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.common.web.entity.GlobalResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cizhong.wcs
 * @date 2022/3/15 5:03 下午
 */
@Slf4j
@RestController
public class HttpErrorHandler implements ErrorController {
    private final static String DEFAULT_ERROR_PATH = "/error";

    @RequestMapping(value = DEFAULT_ERROR_PATH)
    public GlobalResponse<String> error(HttpServletRequest request, HttpServletResponse response) {
        GlobalResponse<String> result = new GlobalResponse<>();
        result.setCode(AmapExceptionCode.COMMON_REQUEST_ERROR.getCode());
        result.setMessage("request error: " + response.getStatus());
        return result;
    }

    @Override
    public String getErrorPath() {
        return DEFAULT_ERROR_PATH;
    }
}
